package com.example.rentingapp.activity;

import android.view.View;
import android.widget.RadioButton;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.rentingapp.R;

import android.os.Bundle;
import android.widget.RadioButton;

import com.example.rentingapp.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager2 viewPager2;
    private MyFragmentPagerAdapter pagerAdapter;
    private RadioButton rb_home, rb_search, rb_message, rb_mine, rb_current;

    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        rb_home = findViewById(R.id.tab_home);
        rb_search = findViewById(R.id.tab_search);
//        rb_message = findViewById(R.id.tab_message);
        rb_mine = findViewById(R.id.tab_mine);
    }

    @Override
    protected void initData() {
        rb_home.setOnClickListener(this);
        rb_search.setOnClickListener(this);
//        rb_message.setOnClickListener(this);
        rb_mine.setOnClickListener(this);

        rb_home.setSelected(false);
        rb_current = rb_home;

        initPager();
    }

    //管理Fragment
    private void initPager() {
        viewPager2 = findViewById(R.id.viewpager2);

        pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle());
        viewPager2.setAdapter(pagerAdapter);
        viewPager2.setUserInputEnabled(false);//设置禁止滑动

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }
        });
    }

    private void changeTab(int position) {
        //rb_current为当前的按钮
        rb_current.setSelected(false);
        switch (position) {
            case R.id.tab_home:
                viewPager2.setCurrentItem(0, false);
            case 0:
                rb_home.setSelected(true);
                rb_current = rb_home;
                break;
            case R.id.tab_search:
                viewPager2.setCurrentItem(1, false);
            case 1:
                rb_search.setSelected(true);
                rb_current = rb_search;
                break;
//            case R.id.tab_message:
//                viewPager2.setCurrentItem(2, false);
//            case 2:
//                rb_message.setSelected(true);
//                rb_current = rb_message;
//                break;
            case R.id.tab_mine:
                viewPager2.setCurrentItem(2, false);
            case 2:
                rb_mine.setSelected(true);
                rb_current = rb_mine;
                break;
        }
    }

    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}